package com.pzh.logistics.controller;

import com.pzh.logistics.common.ResultVO;
import com.pzh.logistics.common.constant.RoleConstant;
import com.pzh.logistics.exception.GlobalException;
import com.pzh.logistics.exception.ResultEnum;
import com.pzh.logistics.mbg.model.TransitOrder;
import com.pzh.logistics.mbg.model.User;
import com.pzh.logistics.pojo.VO.OrderVO;
import com.pzh.logistics.service.OrderService;
import com.pzh.logistics.util.ResultVOUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 代码目录：
     * - 流程相关：订单相关的流程操作
     * - 信息相关：订单信息相关的操作
     *
     */

    // 流程相关 ======================================================

    /**
     * 用户下单
     * @param form
     * @return
     */
    @RequestMapping("/place")
    @RequiresRoles(RoleConstant.USER)
    public ResultVO placeOrder(@RequestBody TransitOrder form) {
        User user = getLoginUser();
        form.setUserId(user.getUserId());
        OrderVO orderVO = orderService.placeOrder(form);
        return ResultVOUtil.success(orderVO);
    }

    /**
     * 用户方（寄件人）付款
     * @param orderId
     * @return
     */
    @RequestMapping("/senderPay")
    @RequiresRoles(RoleConstant.USER)
    public ResultVO senderPay(Integer orderId) {
        User user = getLoginUser();
        orderService.senderPay(orderId, user.getUserId());
        return ResultVOUtil.success();
    }

    /**
     * 揽件确认
     * @param orderId
     * @return
     */
    @RequestMapping("/collectConfirm")
    @RequiresRoles(RoleConstant.COURIER)
    public ResultVO collectConfirm(Integer orderId) {
        User user = getLoginUser();
        orderService.collectConfirm(orderId, user.getRoleRelativeId());
        return ResultVOUtil.success();
    }

    /**
     * 到达站点确认
     * @param idList
     * @return
     */
    @RequestMapping("/arriveStationConfirm")
    @RequiresRoles(RoleConstant.STATION)
    public ResultVO arriveStationConfirm(List<Integer> idList) {
        User user = getLoginUser();
        orderService.arriveStationConfirm(idList, user.getRoleRelativeId());
        return ResultVOUtil.success();
    }

    /**
     * 到达中心确认
     * @param idList
     * @return
     */
    @RequestMapping("/arriveCenterConfirm")
    @RequiresRoles(RoleConstant.STATION)
    public ResultVO arriveCenterConfirm(List<Integer> idList) {
        User user = getLoginUser();
        orderService.arriveStationConfirm(idList, user.getRoleRelativeId());
        return ResultVOUtil.success();
    }

    /**
     * 快递员确认送达
     * @param orderId
     * @return
     */
    @RequestMapping("/signConfirm")
    @RequiresRoles(RoleConstant.COURIER)
    public ResultVO signConfirm(Integer orderId) {
        orderService.signConfirm(orderId);
        return ResultVOUtil.success();
    }

    /**
     * 到付：收件人签收付款后，快递员确认付款
     * @param orderId
     * @return
     */
    @RequestMapping("/recipientPay")
    @RequiresRoles(RoleConstant.COURIER)
    public ResultVO recipientPayOrder(Integer orderId) {
        orderService.recipientPay(orderId);
        return ResultVOUtil.success();
    }

    /**
     * 用户方订单完成确认
     * @param orderId
     * @return
     */
    @RequestMapping("/orderComplete")
    @RequiresRoles(RoleConstant.USER)
    public ResultVO orderComplete(Integer orderId) {
        User user = getLoginUser();
        orderService.orderCompleteConfirm(orderId, user.getUserId());
        return ResultVOUtil.success();
    }


    // 订单信息相关 ======================================================

    /**
     * 用户查看个人订单信息
     * @return
     */
    @RequestMapping("/InfoOfUser")
    @RequiresRoles(RoleConstant.USER)
    public ResultVO userOrderInfo() {
        User user = getLoginUser();
        return ResultVOUtil.success(orderService.findByUserId(user.getUserId()));
    }

    /**
     * 查看订单详情
     * @return
     */
    @RequestMapping("/orderDetail")
    public ResultVO orderDetail(Integer orderId) {
        User user = getLoginUser();
        TransitOrder transitOrder = orderService.findByOrderId(orderId);
        if (RoleConstant.USER.equals(user.getRole())
                && !transitOrder.getUserId().equals(user.getUserId())){
            throw new GlobalException(ResultEnum.CHECK_ULTRA_VIRES);
        }
        return ResultVOUtil.success(transitOrder);
    }

    // 私有方法 ====================================================
    private User getLoginUser(){
        Subject subject = SecurityUtils.getSubject();
        return (User)subject.getPrincipal();
    }

}
